package com.wxtf.platform.main.contraceptives;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.interfaces.CommonsCallProvider;
import com.wxtf.platform.plugins.user.UserBean;
import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.AccessDataBaseUtil;
import com.wxtf.platform.tools.ConstantInfoHelper;
import com.wxtf.platform.tools.FormatUtil;
/**
 * <li>Title: TmaStoreCheckMainAction.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.main.contraceptives</li>
 * <li>Description: 药具盘点主表</li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Jul 5, 2012 10:08:48 AM</li>
 *
 * @author liu_zhaojun
 * @version 1.0.0.0
 *
 */
public class TmaStoreCheckMainAction extends LogicBase implements
		CommonsCallProvider {
	
	private static Log log = LogFactory.getLog(TmaStoreCheckMainAction.class);
	
	public TmaStoreCheckMainAction(String jsonArray) {

	}
	
	@Override
	public Object delete(Object jsonArray) throws Exception {
		return null;
	}

	/** 
	 * 描述：insert 增加盘点记录
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @CreateOn Jul 10, 2012  5:12:42 PM
	 * @author liu_zhaojun
	 */
	@Override
	public Object insert(Object jsonArray) throws Exception {		
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		UserBean user = (UserBean)(this.getRequest()).getSession().getAttribute("userBean");
		String org_id = user.getCurOrgId();
		String tmascm_code = jsonObject.getString("tmascm_code");
		sql = "select count(*) from tma_store_check_main where tmascm_code=?";
		int hasId = AccessDataBaseUtil.queryForInt(sql,new Object[]{tmascm_code});
		if(hasId>0){
			return "\"盘点编号不能重复\"";
		}
		String create_time = FormatUtil.getCurentDate();				
		String tmascm_id = AccessDataBaseUtil.getPKUUID();
		String create_by = user.getUserId();
		List<String> list = new ArrayList<String>();
		StringBuffer buff = new StringBuffer("insert into tma_store_check_main(tmascm_id,org_id,tmascm_type,tmascm_code,tmascm_name,tmascm_time,tmascm_is_valid,tmascm_memo,create_by,create_time,last_modify_by,last_modify_time) values(");
		buff.append("'").append(tmascm_id).append("'")
		.append(",'").append(org_id).append("'")
		.append(",'").append(jsonObject.getString("tmascm_type")).append("'")
		.append(",'").append(tmascm_code).append("'")
		.append(",'").append(jsonObject.getString("tmascm_name")).append("'")
		.append(",'").append(create_time.substring(0, 7)).append("'")
		.append(",'").append(jsonObject.getString("tmascm_is_valid")).append("'")
		.append(",'").append(jsonObject.getString("tmascm_memo")).append("'")
		.append(",'").append(create_by).append("'")
		.append(",'").append(create_time).append("'")
		.append(",'").append(create_by).append("'")
		.append(",'").append(create_time).append("')");
		list.add(buff.toString());
		buff.setLength(0);
		String ids = jsonObject.getString("ids");
		array = JSONArray.fromObject(ids);
		for(int i=0;i<array.size();i++){
			jsonObject = array.getJSONObject(i);
			buff.append("insert into tma_store_check_detail(tmascd_id,tmascm_id,tmad_id,back_tmascd_num,tmaid_num,tmaod_num,tmascd_num,tmascd_num_y,tmascd_num_s,tmascd_num_c,create_by,create_time,last_modify_by,last_modify_time) values(")
			.append("'").append(AccessDataBaseUtil.getPKUUID()).append("'")
			.append(",'").append(tmascm_id).append("'")
			.append(",'").append(jsonObject.getString("tmad_id")).append("'")
			.append(",'").append(jsonObject.getString("back_tmascd_num")).append("'")
			.append(",'").append(jsonObject.getString("tmaid_num")).append("'")
			.append(",'").append(jsonObject.getString("tmaod_num")).append("'")
			.append(",'").append(jsonObject.getString("num")).append("'")
			.append(",'").append(jsonObject.getString("tmascd_num_yk")).append("'")
			.append(",'").append(jsonObject.getString("tmascd_num_s")).append("'")
			.append(",'").append(jsonObject.getString("tmascd_num_c")).append("'")
			.append(",'").append(create_by).append("'")
			.append(",'").append(create_time).append("'")
			.append(",'").append(create_by).append("'")
			.append(",'").append(create_time).append("')");
			list.add(buff.toString());
			buff.setLength(0);
		}
		AccessDataBaseUtil.executeSQLUpdate(list);
		return "\"新建成功\"";
	}

	@Override
	public Object recordCount(Object jsonArray) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object run() {
		Object msg = "";
		try {
			if ("queryPage".equals(this.getMethodName())) {// 列表
				msg = this.select(this.getLogicData());
			} else if ("save".equals(this.getMethodName())) {// 添加
				msg = this.insert(this.getLogicData());
			} else if ("update".equals(this.getMethodName())) {// 添加
				msg = this.update(this.getLogicData());
			} else if ("yuefen".equals(this.getMethodName())) {// 列表
				msg = this.yuefen(this.getLogicData());
			} else if ("zaicipandian".equals(this.getMethodName())) {// 列表
				msg = this.zaicipandian(this.getLogicData());
			} else {
				msg = "\"没有【" + this.getMethodName() + "】该方法\"";
			}
			if(msg == null || "[null]".equals(msg)){
				msg = "\"\"";
			}
			if(msg.toString().startsWith("{") || msg.toString().startsWith("[")){
				return msg;
			}
			msg = ConstantInfoHelper.generateJSONString(msg.toString());
			//log.info(this.getClass().getName() +":"+this.getMethodName() + " -> " + msg);
		} catch (Exception e) {
			msg = "操作失败，请联系管理员";
			msg = ConstantInfoHelper.generateJSONString(ConstantInfoHelper.JSON_HEAD, ConstantInfoHelper.getFailResultDetail(msg.toString()), false);
			log.error(e, e);
		} finally {
			this.clear();
		}
		return msg.toString();
	}

	/** 
	 * 描述：select 查询盘点记录
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @CreateOn Jul 10, 2012  5:12:42 PM
	 * @author liu_zhaojun
	 */
	@Override
	public Object select(Object jsonArray) throws Exception {
		UserBean user = (UserBean)(this.getRequest()).getSession().getAttribute("userBean");
		String org_id = user.getCurOrgId();
		sql = " from tma_store_check_main s,t_department d,t_user t where s.org_id = d.department_id and s.create_by=t.user_id and s.org_id=?";
		String countSql = "select count(s.tmascm_id)" + sql;
		String querySql = "select s.tmascm_id,t.caption as create_by,d.caption,s.tmascm_code,s.tmascm_type,s.tmascm_name,s.tmascm_time,s.tmascm_is_valid" + sql;
		String[] fields = {"tmascm_id","create_by","caption","tmascm_code","tmascm_type","tmascm_name","tmascm_time","tmascm_is_valid"};
		return AccessDataBaseUtil.query4SQLPagging(fields, countSql, querySql, this.getPage(), this.getPagesize(), new Object[]{org_id});
	}
	
	/** 
	 * 描述：yuefen 本月是否以盘点
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @CreateOn Jul 10, 2012  5:12:42 PM
	 * @author liu_zhaojun
	 */
	public Object yuefen(Object jsonArray) throws Exception {
		UserBean user = (UserBean)(this.getRequest()).getSession().getAttribute("userBean");
		String org_id = user.getCurOrgId();
		String time = FormatUtil.getCurentDate().substring(0, 7);
		StringBuffer buff = new StringBuffer();
		buff.append("select count(*) from tma_store_check_main where tmascm_time like '%").append(time).append("%'")
		.append(" and org_id='").append(org_id).append("'");
		int hasId = AccessDataBaseUtil.queryForInt(buff.toString(),null);
		if(hasId>0){
			return "\"每个月只能新建一次盘点\"";
		}
		return "0";
	}
	
	/** 
	 * 描述：zaicipandian 是否可以再次盘点
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @CreateOn Jul 10, 2012  5:12:42 PM
	 * @author liu_zhaojun
	 */
	public Object zaicipandian(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String tmascm_id = jsonObject.getString("tmascm_id");
		UserBean user = (UserBean)(this.getRequest()).getSession().getAttribute("userBean");
		String org_id = user.getCurOrgId();
		StringBuffer buff = new StringBuffer();
		buff.append("select tmascm_is_valid from tma_store_check_main where tmascm_id='").append(tmascm_id).append("'")
		.append(" and org_id='").append(org_id).append("'");
		int hasId = AccessDataBaseUtil.queryForInt(buff.toString(),null);
		if(hasId>0){
			return "\"已提交盘点不能再次盘点\"";
		}
		return "0";
	}

	@Override
	public Object setRequest(HttpServletRequest request) throws Exception {
		this.request = request;
		return request;
	}
	/** 
	 * 描述：update 调整库存
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @CreateOn Jul 10, 2012  5:12:42 PM
	 * @author liu_zhaojun
	 */
	@Override
	public Object update(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		UserBean user = (UserBean)(this.getRequest()).getSession().getAttribute("userBean");
		String org_id = user.getCurOrgId();
		String create_time = FormatUtil.getCurentDate();
		String create_by = user.getUserId();
		List<String> list = new ArrayList<String>();
		String tmascm_id = jsonObject.getString("tmascm_id");
		sql = "delete from tma_store_check_detail where tmascm_id=?";
		AccessDataBaseUtil.deleteById(sql, tmascm_id);
		StringBuffer buff = new StringBuffer("update tma_store_check_main set ");
		buff.append("org_id='").append(org_id).append("',")
		.append("tmascm_type='").append(jsonObject.getString("tmascm_type")).append("',")
		.append("tmascm_code='").append(jsonObject.getString("tmascm_code")).append("',")
		.append("tmascm_name='").append(jsonObject.getString("tmascm_name")).append("',")
		.append("tmascm_is_valid='").append(jsonObject.getString("tmascm_is_valid")).append("',")
		.append("tmascm_memo='").append(jsonObject.getString("tmascm_memo")).append("',")
		.append("last_modify_by='").append(create_by).append("',")
		.append("last_modify_time='").append(create_time).append("'")
		.append(" where tmascm_id='").append(tmascm_id).append("'");
		list.add(buff.toString());
		buff.setLength(0);
		String ids = jsonObject.getString("ids");
		array = JSONArray.fromObject(ids);
		for(int i=0;i<array.size();i++){
			jsonObject = array.getJSONObject(i);
			buff.append("insert into tma_store_check_detail(tmascd_id,tmascm_id,tmad_id,back_tmascd_num,tmaid_num,tmaod_num,tmascd_num,tmascd_num_y,tmascd_num_s,tmascd_num_c,create_by,create_time,last_modify_by,last_modify_time) values(")
			.append("'").append(AccessDataBaseUtil.getPKUUID()).append("'")
			.append(",'").append(tmascm_id).append("'")
			.append(",'").append(jsonObject.getString("tmad_id")).append("'")
			.append(",'").append(jsonObject.getString("back_tmascd_num")).append("'")
			.append(",'").append(jsonObject.getString("tmaid_num")).append("'")
			.append(",'").append(jsonObject.getString("tmaod_num")).append("'")
			.append(",'").append(jsonObject.getString("num")).append("'")
			.append(",'").append(jsonObject.getString("tmascd_num_yk")).append("'")
			.append(",'").append(jsonObject.getString("tmascd_num_s")).append("'")
			.append(",'").append(jsonObject.getString("tmascd_num_c")).append("'")
			.append(",'").append(create_by).append("'")
			.append(",'").append(create_time).append("'")
			.append(",'").append(create_by).append("'")
			.append(",'").append(create_time).append("')");
			list.add(buff.toString());
			buff.setLength(0);
		}
		AccessDataBaseUtil.executeSQLUpdate(list);
		return "\"修改成功\"";
	}

	@Override
	public Object updateBatch(Object jsonArray) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object onCreate(Object jsonArray) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object onDestory(Object jsonArray) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object onInit(Object jsonArray) {
		// TODO Auto-generated method stub
		return null;
	}

}
